package com.hexanet.commons;

import java.util.Date;

import com.hexanet.bom.Cell;

public class EnergyCommonsService {
    
    public int calculateProducedDeltaEnergy(Cell c) {
	
	Date lastUpdatd = c.getSavedEnergyUpdated();	
	Long diffMillis = new Date().getTime() - lastUpdatd.getTime();
	Long diffMinutes = diffMillis / 60000;	
	
	int minutes = diffMinutes.intValue();	
	int energyIntervall = HexanetConstants.getEnergyIntervalForCellLevel(c.getLevel());
	int producedItems = minutes / energyIntervall;
	
	int produced = c.getProducesEnergyNetto() * producedItems;
	return produced;
    }
    
    public void calculateProducedEnergy(Cell c) {
	
	Date lastUpdatd = c.getSavedEnergyUpdated();	
	Long diffMillis = new Date().getTime() - lastUpdatd.getTime();
	Long diffMinutes = diffMillis / 60000;
	
	
	int minutes = diffMinutes.intValue();	
	int energyIntervall = HexanetConstants.getEnergyIntervalForCellLevel(c.getLevel());
	int producedItems = minutes / energyIntervall;
	int minutesRest = minutes % energyIntervall;
	long millisRest = minutesRest * 60000;
	int produced = c.getProducesEnergyNetto() * producedItems;
	
	Date now = new Date();
	Date updatedNew = new Date(now.getTime() - millisRest);
	
	c.setSavedEnergy(c.getSavedEnergy() + produced);
	c.setSavedEnergyUpdated(updatedNew);
	
    }

}
